package com.summer.shopping.controller;

import com.summer.shopping.domain.table.dto.PageDTO;
import com.summer.shopping.domain.table.vo.ShoppingCartVo;
import com.summer.shopping.utils.PageResult;
import com.summer.shopping.utils.Result;
import com.summer.shopping.domain.table.ShoppingCart;
import com.summer.shopping.domain.table.dto.ShoppingCarDTO;
import com.summer.shopping.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    @PostMapping("add")
    public Result<String> add(@RequestBody ShoppingCarDTO shoppingCartDTO){
        log.info("添加购物车：{}",shoppingCartDTO);
        shoppingCartService.add(shoppingCartDTO);
        return Result.ok("添加成功");
    }

    @GetMapping("list")
    public Result<PageResult<ShoppingCartVo>> list(PageDTO pageDTO){
        log.info("查询购物车:{}",pageDTO);
        PageResult<ShoppingCartVo> shoppingCartVoPageResult=shoppingCartService.getPageList(pageDTO);
        return Result.ok(shoppingCartVoPageResult);
    }

    @DeleteMapping("clean")
    public Result<String> clean(){
        log.info("清空购物车");
        shoppingCartService.clean();
        return Result.ok("清空成功");
    }

    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("批量删除购物车:{}",ids);
        shoppingCartService.delete(ids);
        return Result.ok("批量删除成功");
    }
}
